package us.westudio.manage.database;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;

public class ClassroomDbHandler {

	private DatabaseHandler database;
	
	public ClassroomDbHandler(Context context){
		this.database = new DatabaseHandler(context, DatabaseHelper.TABLE_NAME);
	}
	
	//get information by week
	public ArrayList<Classroom> gainInfoByWeek(int week){
		ArrayList<Classroom> result = new ArrayList<Classroom>();
		try {
			this.database.setReadableDatabase();
			ArrayList<HashMap<String, String>>selectResult = this.database.select(new String[]{"day","part","classmeg"}, "week='"+week+"'");
			for(HashMap<String, String> selectResultMap : selectResult){
				result.add(new Classroom(week, Integer.parseInt(selectResultMap.get("day")), Integer.parseInt(selectResultMap.get("part")), selectResultMap.get("classmeg")));
			}
			this.database.closeDatabase();
		} catch (Exception e) {
			
		}
		return result;
	}
	
	//get information by week and day
	public ArrayList<Classroom>gainInfoByWeekAndDay(int week, int day){
		ArrayList<Classroom>result = new ArrayList<Classroom>();
		try {
			this.database.setReadableDatabase();
			ArrayList<HashMap<String, String>>selectResult = this.database.select(new String[]{"part", "classmeg"}, "week='"+week+"' AND day='"+day+"'");
			for(HashMap<String, String> selectResultMap : selectResult){
				result.add( new Classroom(week, day, Integer.parseInt(selectResultMap.get("part")), selectResultMap.get("classmeg")) );
			}
			this.database.closeDatabase();
		} catch (Exception e) {
			
		}
		return result;
	}
	
	//get classroom detail information
	public ArrayList<Classroom>gainInfoByDetail(int week, int day, int section){
		ArrayList<Classroom>result = new ArrayList<Classroom>();
		try {
			this.database.setReadableDatabase();
			ArrayList<HashMap<String, String>>selectResult = this.database.select("classmeg", "week='"+week+"' AND day='"+day+"' AND part='"+section+"'");
			for(HashMap<String, String> selectResultMap : selectResult){
				result.add( new Classroom(week, day, section, selectResultMap.get("classmeg")) );
			}
			this.database.closeDatabase();
		} catch (Exception e) {
			
		}
		return result;
	}
	
	//insert classroom info
	public boolean insertClassroomInfo(Classroom classroom){
		return this.insertClassroomInfo(classroom.week, classroom.day, classroom.section, classroom.information);
	}
	
	public Boolean insertClassroomInfo(int week, int day, int section, String information ){
		boolean status = true;
		ContentValues insertValues = new ContentValues();
		insertValues.put("week", week);
		insertValues.put("day", day);
		insertValues.put("part", section);
		insertValues.put("classmeg", information);
			
		try {
			this.database.setWritableDatabase();
			if( this.database.insert(insertValues) == false)
				status = !status;
			this.database.closeDatabase();
		} catch (Exception e) {
			status = !status;
		}
		return status;
	}
	
}
